package com.wsoft.bpm.bean;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;

import java.io.Serializable;

/**
 * 会签功能元素
 *
 * @Author zhonglj
 * @Date 2025/2/21 11:05
 */
@Data
public class CountersignElement implements Serializable {
    private static final long serialVersionUID = 1L;

    public final static int COMPLETE_RESULT_RUNNING = 0;
    public final static int COMPLETE_RESULT_AGREE = 1;
    public final static int COMPLETE_RESULT_REFUSE = 2;
    @Schema(description = "同意数")
    private int agree;
    @Schema(description = "拒绝数")
    private int refuse;
    @Schema(description = "总数")
    private int total;
    @Schema(description = "完成表达式")
    private String completionExpression;

    /**
     * 计算当前节点审批结果
     * COMPLETE_RESULT_RUNNING = 0;
     * COMPLETE_RESULT_AGREE = 1;
     * COMPLETE_RESULT_REFUSE = 2;
     *
     * @return int
     * @author zhonglj
     * @date 2025/2/21 11:05
     **/
    public int completeResult() {
        Expression exp = new ExpressionBuilder(completionExpression).build();
        double evaluate = exp.evaluate();
        double totalDouble = total * 1.0;
        if (agree + refuse < totalDouble) {
            // 未完成
            return COMPLETE_RESULT_RUNNING;
        } else if (agree / totalDouble >= evaluate) {
            // 同意率达到表达式要求
            return COMPLETE_RESULT_AGREE;
        } else {
            return COMPLETE_RESULT_REFUSE;
        }
    }

    public void increaseAgree() {
        agree++;
    }

    public void increaseRefuse() {
        refuse++;
    }
}
